<?php

namespace App\Jobs;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

/**
 * Class payNotifyJob
 * @package App\Jobs
 */
class PayNotifyJob extends Job
{
    /**
     * @var int
     */
    protected $tries = 3;

    /**
     * @var int
     */
    protected $timeout = 60;

    /**
     * @var string
     */
    protected $url;

    /**
     * @var array
     */
    protected $params;

    /**
     * PayCallbackJob constructor.
     * @param string $url
     * @param array $params
     */
    public function __construct($url, $params)
    {
        $this->url = $url;
        $this->params = $params;
    }

    /**
     * Execute the job.
     * @throws \Exception
     */
    public function handle()
    {
        $response = (new Client())->post($this->url, [
            'form_params' => $this->params,
            'verify'      => false
        ]);

        $code = $response->getStatusCode();
        $contents = $response->getBody()->getContents();
        if ($code != 200 || strtoupper($contents) != 'OK') {
            throw new \Exception('回调响应验证失败');
        }
    }
}
